<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录页面</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .all {
            width: 300px;
            height: 380px;
            border: 1px solid #333333;
            text-align: center;
        }
        
        h2 {
            text-align: center;
            margin-top: 30px;
            margin-bottom: 30px;
        }
        
        p {
            width: 100%;
            height: 50px;
            margin-bottom: 15px;
        }
        
        input {
            width: 90%;
            height: 40px;
            outline: none;
            line-height: 40px;
        }
        
        .last {
            display: flex;
            align-items: center;
            /* justify-content: center; */
            margin-left: 20px;
        }
        
        #remember {
            width: 30px;
            height: 30px;
        }
        
        button {
            width: 90%;
            height: 40px;
            background-color: rgba(48, 112, 231, 0.7);
            border: none;
            border-radius: 10px;
            color: #ffffff;
            font-size: 20px;
            transition: all .5s;
        }
        
        button:hover {
            background-color: rgba(48, 112, 231);
        }
        
        a {
            text-decoration: none;
            color: rgb(78, 129, 223, .6);
            transition: all .5s;
        }
        
        a:hover {
            color: rgb(78, 129, 223);
        }
    </style>
</head>

<body>
    <div class="all">
        <h2>账号登录</h2>
        <p>
            <input type="text" placeholder="请输入您的用户名" id="username">
        </p>
        <p>
            <input type="password" placeholder="请输入您的密码" id="password">
        </p>
        <p class="last">
            <input type="checkbox" id="remember">&nbsp;&nbsp;&nbsp;
            <label for="remember">记住用户名和密码</label>
        </p>
        <p>
            <button id="gologin">登&nbsp;&nbsp;录</button>
        </p>
        <p>
            <a href="register.html">注册账号</a>
        </p>
    </div>
</body>

</html>
<script src="jquery.min.js"></script>

<script>
    $(function() {
        let username = localStorage.getItem("username");
        let password = localStorage.getItem("password");

        if (username && password) {
            $("#username").val(username);
            $("#password").val(password);
            $("#gologin").click()
        }
    })

    $("#gologin").click(function() {
        if ($("#username").val() && $("#password").val()) {
            let uname = $("#username").val();
            let pwd = $("#password").val();

            $.ajax({
                url: "http://10.35.164.244:8080/goLogin",
                data: {
                    username: uname,
                    password: pwd
                },
                success(data) {
                    if (data === "none") {
                        alert("该用户名未注册，请前往注册页面进行注册")
                    }
                    if (data === "success") {
                        alert("登录成功");

                        //永久存储
                        if ($("#remember")[0].checked) {
                            localStorage.setItem("username", uname);
                            localStorage.setItem("password", pwd);
                        }
                        location.href = "http://10.35.164.244/subLocalStorage/success.html";
                    } else {
                        alert("用户名或密码输入错误");
                    }
                }
            })

        }
    })
</script>